﻿using System.Windows;
using FirstFloor.ModernUI.Windows.Controls;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using Tisbi.LawFirm.BL;
using Tisbi.LawFirm.BL.Services.Implementation;
using Tisbi.LawFirm.UI.ViewModel;
using Tisbi.LawFirm.UI.Views.Controls.Forms;

namespace Tisbi.LawFirm.UI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var PopUpDialog = new ModernDialog
            {
                Title = "Авторизация",
                Content = new LogInWindow()
            };

            PopUpDialog.Buttons = new[] { PopUpDialog.OkButton, PopUpDialog.CancelButton };

            PopUpDialog.OkButton.Content = "Вход";
            PopUpDialog.CancelButton.Content = "Отмена";

            PopUpDialog.OkButton.Command = new RelayCommand(() =>
            {
                if (!DBContextManager.HasConnected())
                {
                    Notifyer.NotifyWithActino("Нет подключения к БД, возможно не заданы настройки.", () =>
                    {
                        var settingDialog = new ModernDialog
                        {
                            Title = "Настройки подключения к БД",
                            Content = new SqlConnectionStringBuilder(),
                            Buttons = new[] { PopUpDialog.OkButton, PopUpDialog.CancelButton }
                        };

                        settingDialog.OkButton.Content = "Сохранить";
                        PopUpDialog.CancelButton.Content = "Отмена";

                        PopUpDialog.OkButton.Command = new RelayCommand(() =>
                        {
                            var model = ServiceLocator.Current.GetInstance<SqlConnectionStringViewModel>();
                            if (model == null)
                            {
                                Notifyer.NotifyError("Model is null", null);
                                settingDialog.Close();
                                return;
                            }

                            ConfigurationHelper.UpdateConnectionString(model);
                            DBContextManager.Update();
                            settingDialog.Close();
                        });

                        settingDialog.ShowDialog();
                    });
                }

                var loginViewModel = ServiceLocator.Current.GetInstance<LoginViewModel>();
                SecurityContextSingleton.GetInstance().LogIn(loginViewModel.UserName, loginViewModel.Password);

                if (!SecurityContextSingleton.GetInstance().IsAuthenticated)
                {
                    return;
                }

                var mainWindow = new MainWindow();
                mainWindow.Show();
                PopUpDialog.Close();
            });

            PopUpDialog.CancelButton.Command = new RelayCommand(() => Current.Shutdown(-1));
            PopUpDialog.ShowDialog();
        }
    }
}
